package com.sa.commons.tools.check;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DataValidator {

	private static Log log = LogFactory.getLog(DataValidator.class);

	private static String regexFile = DataValidator.class.getClassLoader()
			.getResource("com/sa/commons/tools/check/valiRegex.properties")
			.getPath();

	private static Properties p = new Properties();

	private static Pattern pdateRegex;

	private static Pattern ptimeRegex;

	private static Pattern pdateTimeRegex;

	private static Pattern pemailRegex;

	private static Pattern pidRegex;

	private static Pattern pmobileRegex;

	private static Pattern pnumRegex;

	private static Pattern ppasswordRegex;

	private static Pattern ptelRegex;

	private static Pattern pqqRegex;

	private static Map<String, Pattern> pmap = new HashMap<String, Pattern>();

	/**
	 * 加载配置文件
	 */
	static {
		buildProperties();
		initPmap();
		initPattern();
		log.debug("数据验证正则表达式加载完毕！");
	}

	/**
	 * 初始化Properties对象
	 */
	static void buildProperties() {
		FileInputStream in;

		File file = new File(regexFile);
		try {
			in = new FileInputStream(file);
			p.load(in);
			in.close();
		} catch (FileNotFoundException e) {
			log.error("找不到数据验证文件：" + regexFile + "！");
		} catch (IOException e) {
			log.error("读取配置文件数据验证文件错误：" + regexFile + "！", e);
		}
	}

	/**
	 * 构造pmap
	 */
	static void initPmap() {
		for (Map.Entry<Object, Object> entry : p.entrySet()) {
			String key = (String) entry.getKey();
			String value = (String) entry.getValue();

			if (notNull(value) == null) {
				Pattern pattern = Pattern.compile(value);
				pmap.put(key, pattern);
			} else {
				log.warn("数据验证配置文件：" + regexFile + "中,key=" + key + "的数据为空！");
			}

		}
	}

	static void initPattern() {
		pdateRegex = getPattern("date");

		ptimeRegex = getPattern("time");

		pdateTimeRegex = getPattern("dateTime");

		pemailRegex = getPattern("email");

		pidRegex = getPattern("id");

		pmobileRegex = getPattern("mobile");

		pnumRegex = getPattern("num");

		ppasswordRegex = getPattern("password");

		ptelRegex = getPattern("tel");

		pqqRegex = getPattern("qq");
	}

	public static String getRegex(String key) {
		String regex = p.getProperty(key);
		if (regex != null) {
			return regex;
		}
		log.error("不存在key=" + key + "的正则表达式样式！");
		return null;
	}

	/**
	 * 获取正则表达式样式
	 * 
	 * @param key
	 * @return
	 */
	static Pattern getPattern(String key) {
		Pattern pattern = pmap.get(key);
		if (pattern == null) {
			log.error("不存在key=" + key + "的正则表达式样式！");
			return null;
		}
		return pattern;
	}

	/**
	 * 判断data是否满足pattern的格式
	 * 
	 * @param data
	 * @param pattern
	 * @return
	 */
	public static boolean pmatch(String data, Pattern pattern) {
		Matcher m = pattern.matcher(data);
		return m.matches();
	}

	/**
	 * 使用pmap中指定key的正则表达式验证data<br>
	 * 
	 * @param key
	 * @param data
	 * @return
	 */
	public static boolean pmapVali(String key, String data) {
		Matcher m = getPattern(key).matcher(data);
		return m.matches();
	}

	public static String notNull(String data) {
		if (data == null || "".equals(data.trim())) {
			return "字段不能为空！";
		}

		return null;
	}

	public static String valiDate(String date) {
		if (pmatch(date, pdateRegex)) {
			return null;
		}
		return "日期格式错误！";
	}

	public static String valiDateTime(String dateTime) {
		if (pmatch(dateTime, pdateTimeRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiEmail(String email) {
		if (pmatch(email, pemailRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiId(String id) {
		if (pmatch(id, pidRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiLatitude(Double latitude) {

		return "格式错误！";
	}

	public static String valiLongitude(Double longitude) {

		return "格式错误！";
	}

	public static String valiMobile(String mobile) {
		if (pmatch(mobile, pmobileRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiQQ(String qq) {
		if (pmatch(qq, pqqRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public  static String valiNum(String num) {
		if (pmatch(num, pnumRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiPassword(String password) {
		if (pmatch(password, ppasswordRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiStartEndDate(Date startDate, Date endDate) {
		return "格式错误！";
	}

	public static String valiStrLen(String data) {

		return "格式错误！";
	}

	public static String valiTel(String tel) {
		if (pmatch(tel, ptelRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static String valiTime(String time) {
		if (pmatch(time, ptimeRegex)) {
			return null;
		}
		return "格式错误！";
	}

	public static void main(String[] args) {
		String regex = "([1|2][0-9][0-9][0-9]) ([0-2][0-4]):(([0-5][0-9])|([6][0])):(([0-5][0-9])|([6][0]))";
		String data = "2009 24:11:59";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(data);
		System.out.println(m.matches());
	}
}
